define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'csmitsm_xcore'], function ($, undefined, Backend, Table, Form, xcore) {

    var Controller = {
        _queryString: '',
        index: function () {

            $("#btn-xcflowgroup").click(function () {
                xcore.openWindow(Fast.api.fixurl("csmitsm/xcflow_group/index"));
            });

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'csmitsm/xcflow/index' + Controller._queryString,
                    add_url: 'csmitsm/xcflow/add' + Controller._queryString,
                    add2_url: 'csmitsm/xcflow/add2' + Controller._queryString,
                    // edit_url: 'csmitsm/xcflow/edit',
                    del_url: 'csmitsm/xcflow/del',
                    multi_url: 'csmitsm/xcflow/multi',
                    import_url: 'csmitsm/xcflow/import',
                    table: 'csmitsm_xcflow',
                }
            });

            $(".btn-add-flow").click(function () {
                Fast.api.open(Fast.api.fixurl("csmitsm/xcflow/add?xcformtype=flow&addtabs=1&status=&dialog=1"),"新增流程");
            });
            $(".btn-add-flowsub").click(function () {
                Fast.api.open(Fast.api.fixurl("csmitsm/xcflow/add?xcformtype=flowsub&addtabs=1&status=&dialog=1"),"新增流程子表");
            });

            Fast.config.openArea = ['98%', '98%'];
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [{
                            checkbox: true
                        },
                        {
                            field: 'id',
                            title: __('Id')
                        },
                        {
                            field: 'xcformtype',
                            title: __('Xcformtype'),
                            searchList: {
                                "flow": __('Xcformtype flow'),
                                "flowsub": __('Xcformtype flowsub')
                            },
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'xcflow_group_name',
                            title: __('Xcflow_group_id')
                        },
                        {
                            field: 'name',
                            title: __('Name'),
                            operate: 'LIKE'
                        },
                        // {field: 'xcflowjson_id', title: __('Xcflowjson_id')},
                        // {field: 'admin_id', title: __('Admin_id')},
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'weigh', title: __('Weigh'), operate: false},
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: {
                                "normal": __('Normal'),
                                "hidden": __('Hidden')
                            },
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [{
                                name: 'click',
                                title: function (row) {
                                    return row.name;
                                },
                                classname: 'btn btn-xs btn-warning btn-click',
                                icon: 'fa fa-folder-o',
                                click: function (data, row) {
                                    xcore.openWindow(Fast.api.fixurl('csmitsm/xcflow/edit/ids/' + row.id) + "?dialog=1");
                                }
                            }]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            Controller.api.rebuildAddUrl(table);
            this._index_bindtab(table);
        },
        _index_bindtab: function (table) {
            //自定义Tab筛选条件
            $('.panel-heading .nav-custom-condition a[data-toggle="tab"]', table.closest(".panel-intro")).on('shown.bs.tab', function (e) {
                var value = $(this).data("value");
                var options = table.bootstrapTable('getOptions');
                var queryParams = options.queryParams;
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    //这一行必须要存在,否则在点击下一页时会丢失搜索栏数据
                    params = queryParams(params);

                    //如果希望追加搜索条件,可使用
                    var filter = params.filter ? JSON.parse(params.filter) : {};
                    var op = params.op ? JSON.parse(params.op) : {};
                    if (value) {
                        //这里可以自定义多个筛选条件
                        filter.xcformtype = value;
                        op.xcformtype = '=';
                    } else {
                        //选全部时要移除相应的条件
                        delete filter.admin_id;
                        delete op.admin_id;
                    }

                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);

                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify(value?{admin_id: value}:{});
                    //params.op = JSON.stringify(value?{admin_id: '='}:{});
                    return params;
                };

                table.trigger("uncheckbox");
                table.bootstrapTable('refresh', {
                    pageNumber: 1
                });
                return false;
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        mdmconfig: function () {

            $(document).on("fa.event.appendfieldlist", "#mdmconfig-form .btn-append", function (e, obj) {
                Form.events.selectpage(obj);
            });

            $("#c-selectmdmtablename").change(function () {});

            Form.api.bindevent($("form[role=form]"), function () {
                location.reload();
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            queryString: function () {
                return location.search.replace("dialog=1", "").split('&').filter(function (item) {
                    return !!item;
                }).join("&");
            },
            rebuildAddUrl: function (table) {
                var $tabs = $('.nav-tabs[data-field]');
                if ($tabs.length > 0) {
                    var field = $tabs.data("field");
                    var options = table.bootstrapTable('getOptions');
                    table.on("pre-body.bs.table", function () {
                        var activeTab = $('.active a', $tabs);
                        var value = activeTab.data("value");
                        var reg = new RegExp(field + "\=(.*?)");
                        var queryString = location.search
                            .replace("dialog=1", "")
                            .replace(reg, "")
                            .split('&')
                            .filter(function (item) {
                                return !!item;
                            }).join("&");
                        if (queryString.indexOf("?") == 0) {
                            queryString = queryString + "&" + field + "=" + value
                        } else {
                            queryString = queryString + "?" + field + "=" + value
                        }
                        options.extend.add_url = 'csmitsm/xcflow/add' + queryString
                    })
                }
            }
        }
    };
    Controller._queryString = Controller.api.queryString();
    return Controller;
});